﻿using LyyCMS.Core;
using LyyCMS.Core.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using System;
using System.Linq;
using System.Threading.Tasks;

namespace LyyCMS.Web.Areas.Dash.Controllers
{
    [Area("Dash")]
    [Route("Dash/[controller]/[action]")]
    public class ChannelsController : BaseController
    {
        
        public ChannelsController(ILogger<BaseController> loggger, LyyCMS.Core.LyyCMSEntities lyyCMSEntities) : base(loggger, lyyCMSEntities)
        {
        }

        // GET: Dash/Channels
        public async Task<IActionResult> Index()
        {
            var lyyCMSEntities = _context.Channels.Include(c => c.Site);
            return View(await lyyCMSEntities.ToListAsync());
        }

        // GET: Dash/Channels/Details/5
        public async Task<IActionResult> Details(int? id)
        {
            if (id == null)
            {
                return NotFound();
            }

            var channel = await _context.Channels
                .Include(c => c.Site)
                .FirstOrDefaultAsync(m => m.Id == id);
            if (channel == null)
            {
                return NotFound();
            }

            return View(channel);
        }

        // GET: Dash/Channels/Create
        public IActionResult Create()
        {
            ViewData["SiteId"] = new SelectList(_context.Sites, "Id", "Name");
            return View();
        }

        // POST: Dash/Channels/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Create([Bind("Title,UseName,IsEnabled,IsComment,IsPics,IsAnnex,SortId,SiteId,CreationTime,DeletionTime,IsDeleted,CreatorUserId,LastModifierUserId,LastModificationTime,DeleterUserId,Id")] Channel channel)
        {
            if (ModelState.IsValid)
            {
                _context.Add(channel);
                await _context.SaveChangesAsync();
                return RedirectToAction(nameof(Index));
            }
            ViewData["SiteId"] = new SelectList(_context.Sites, "Id", "Name", channel.SiteId);
            return View(channel);
        }

        // GET: Dash/Channels/Edit/5
        public async Task<IActionResult> Edit(int? id)
        {
            if (id == null)
            {
                return NotFound();
            }

            var channel = await _context.Channels.FindAsync(id);
            if (channel == null)
            {
                return NotFound();
            }
            ViewData["SiteId"] = new SelectList(_context.Sites, "Id", "Name", channel.SiteId);
            return View(channel);
        }

        // POST: Dash/Channels/Edit/5
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Edit(int id, [Bind("Title,UseName,IsEnabled,IsComment,IsPics,IsAnnex,SortId,SiteId,DeletionTime,IsDeleted,DeleterUserId,Id")] Channel channel)
        {
            if (id != channel.Id)
            {
                return NotFound();
            }

            if (ModelState.IsValid)
            {
                try
                {
                    channel.LastModificationTime = DateTime.Now;
                    channel.LastModifierUserId = 1;
                    _context.Update(channel);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!ChannelExists(channel.Id))
                    {
                        return NotFound();
                    }
                    else
                    {
                        throw;
                    }
                }
                return RedirectToAction(nameof(Index));
            }
            ViewData["SiteId"] = new SelectList(_context.Sites, "Id", "Name", channel.SiteId);
            return View(channel);
        }

        // GET: Dash/Channels/Delete/5
        public async Task<IActionResult> Delete(int? id)
        {
            if (id == null)
            {
                return NotFound();
            }

            var channel = await _context.Channels
                .Include(c => c.Site)
                .FirstOrDefaultAsync(m => m.Id == id);
            if (channel == null)
            {
                return NotFound();
            }

            return View(channel);
        }

        // POST: Dash/Channels/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> DeleteConfirmed(int id)
        {
            var channel = await _context.Channels.FindAsync(id);
            _context.Channels.Remove(channel);
            await _context.SaveChangesAsync();
            return RedirectToAction(nameof(Index));
        }

        private bool ChannelExists(int id)
        {
            return _context.Channels.Any(e => e.Id == id);
        }
    }
}
